<route lang="json5" type="page">
{
  style: {
    navigationBarTitleText: '店铺设置',
  },
}
</route>

<template>
    <item width="160rpx" rightIcon="arrow-right" title="昵称" @click="inputUpdate('修改店铺名称', 'name', store.store.name)">
        <text> {{ store.store.name }}</text>
    </item>

    <item width="160rpx" title="头像">
        <upload v-model="store.store.avatar" @change="uploadUpdate('avatar')" />
    </item>

    <item width="160rpx" title="营业执照">
        <wd-img width="140rpx" height="140rpx"
            :src="store.store.business_license && store.store.business_license.url ? store.store.business_license.url : ''"
            enable-preview mode="aspectFill" />
    </item>

    <item width="220rpx" rightIcon="arrow-right" title="营业执照注册号"
        @click="inputUpdate('修改营业执照注册号', 'business_license_number', store.store.business_license_number)">
        <text> {{ store.store.business_license_number }}</text>
    </item>

    <item width="160rpx" rightIcon="arrow-right" title="联系电话"
        @click="inputUpdate('修改联系电话', 'phone', store.store.phone)">
        <text> {{ store.store.phone }}</text>
    </item>

    <item width="160rpx" rightIcon="arrow-right" title="优惠信息"
        @click="inputUpdate('修改优惠信息', 'discount', store.store.discount)">
        <text> {{ store.store.discount }}</text>
    </item>

    <item width="160rpx" rightIcon="arrow-right" title="物流信息" tips="请输入物流信息，例如：广东发货"
        @click="inputUpdate('修改物流信息', 'logistics', store.store.logistics)">
        <text> {{ store.store.logistics }}</text>
    </item>

    <item width="160rpx" rightIcon="arrow-right" title="营业时间"
        @click="inputUpdate('修改营业时间', 'business_time', store.store.business_time)">
        <text> {{ store.store.business_time }}</text>
    </item>

    <item width="160rpx" rightIcon="arrow-right" title="地区" @click="district_show = true">
        <text> {{ store.store.province }} {{ store.store.city }} {{ store.store.district }}</text>
    </item>

    <item width="160rpx" rightIcon="arrow-right" title="详细地址"
        @click="inputUpdate('修改详细地址', 'address', store.store.address)">
        <text> {{ store.store.address }}</text>
    </item>

    <item width="160rpx" rightIcon="arrow-right" title="选择位置" @click="chooseLocation()">
        <text> {{ store.store.navigation_address }}</text>
    </item>

    <district v-model:show="district_show" :province_id="store.store.province_id" :city_id="store.store.city_id"
        :district_id="store.store.district_id" :province="store.store.province" :city="store.store.city"
        :district="store.store.district" @submit="district_submit" />
</template>

<script lang="ts" setup>
import { useMessage } from 'wot-design-uni';
import { useStore } from '@/utils/store';
import { http } from '@/utils/http';
import gcoord from 'gcoord';

const store = useStore()
const message = useMessage()
function inputUpdate(title: string, field: string, value: any, type: any = 'text') {
    message.prompt({ title: title, inputValue: value, inputType: type }).then((resp) => {
        http.put('/member/store/store', { field: field, value: resp.value }).then((res: any) => {
            uni.showToast({ title: res.message, icon: "none" })
            if (res.code === 200) {
                store.setMemberData()
            }
        })
    }).catch(() => { })
}

function uploadUpdate(field: string) {
    http.put('/member/store/store', { field: field, value: store.store[field] ? JSON.stringify(store.store[field]) : null }).then((res: any) => {
        uni.showToast({ title: res.message, icon: "none" })
        if (res.code === 200) {
            store.setMemberData()
        }
    })
}

const district_show = ref(false)
function district_submit(e: any) {
    http.put('/member/store/store_district', { province_id: e[0].id, city_id: e[1].id, district_id: e[2].id, province: e[0].text, city: e[1].text, district: e[2].text }).then((res: any) => {
        uni.showToast({ title: res.message, icon: "none" })
        if (res.code === 200) {
            store.setMemberData()
        }
    })
}

function chooseLocation() { // 点击地图
    let result = gcoord.transform([store.store.longitude, store.store.latitude], gcoord.BD09, gcoord.GCJ02);
    uni.chooseLocation({
        longitude: result[0],//经度
        latitude: result[1],//纬度
        success: (res) => {
            if (res.errMsg == "chooseLocation:ok") {
                let result = gcoord.transform([res.longitude, res.latitude], gcoord.GCJ02, gcoord.BD09);
                http.put('/member/store/store_navigation', { longitude: result[0], latitude: result[1], navigation_address: res.address }).then((res: any) => {
                    uni.showToast({ title: res.message, icon: "none" })
                    if (res.code === 200) {
                        store.setMemberData()
                    }
                })
            }
        }
    });
}
</script>

<style scoped>
text {
    font-size: 28rpx;
}
</style>